<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/2/12
 * Time: 1:19 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

/**
 * Baocaophongkhenthuong Model
 */
class VastModelBaocaophongkhenthuong extends JModelForm {

    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        $form = $this->loadForm('com_vast.baocaophongkhenthuong', 'baocaophongkhenthuong', array('control' => 'jform', 'load_data' => true));
        if (empty($form)) {
            return false;
        }
        
        return $form;
    }
     
    public function getKhenThuong()
    {
        $phongid = JRequest::getInt('phongid');
        
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*, d.lydo, d.ngayquyetdinh');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtac AS b ON b.hosocanbo_id = a.id');
        $query->join('INNER', '#__phong AS c ON c.id = b.phong_id');
        $query->join('INNER', '#__khenthuong AS d ON d.hosocanbo_id = a.id');
        $query->where('b.taivi = 1');
        $query->where('c.id = ' . $phongid);
        
        $db->setQuery($query);
        
        return $db->loadObjectList();        
    }   
        
    public function getPhong()
    {
        $id = JRequest::getInt('phongid');
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
    
        $query->select('a.*');
        $query->from('#__phong AS a');
        $query->where('id = ' . $id);
        
        $db->setQuery($query);
        
        return $db->loadObject();            
    }
    
    public function getKhoi() 
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $donvi = $this->getDonvi();
        $phong = $this->getPhong();
        if ($donvi !== NULL)
        {
            $query->select('a.*');
            $query->from('#__khoi AS a');
            $query->where('a.id = ' . $donvi->khoi_id);
            $db->setQuery($query);
            
            return $db->loadObject();            
        }
        elseif ($phong !== NULL)
        {
            $query->select('a.*');
            $query->from('#__donvi AS a');
            $query->where('a.id = ' . $phong->donvi_id);
            $db->setQuery($query);            
            
            $tmp = $db->loadObject();
            if ($tmp !== NULL)
            {
                $query->select('a.*');
                $query->from('#__khoi AS a');
                $query->where('a.id = ' . $tmp->khoi_id);
                $db->setQuery($query);
                
                return $db->loadObject();                            
            }
        }
        
        return NULL;
    }
        
    public function getDonvi() {
        $id = JRequest::getInt('phongid');
      
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->join('INNER', '#__phong AS b ON b.donvi_id = a.id');
        $query->where('b.id = ' . $id);
        $db->setQuery($query);
        return $db->loadObject();

    }                    
}